<html>
<head><meta charset="utf-8"><title>struct usage leading to poor performance · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/struct.20usage.20leading.20to.20poor.20performance.html">struct usage leading to poor performance</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="241928958"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/struct%20usage%20leading%20to%20poor%20performance/near/241928958" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Manuel Drehwald <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/struct.20usage.20leading.20to.20poor.20performance.html#241928958">(Jun 08 2021 at 14:34)</a>:</h4>
<p>I ran into an issue when packing two arrays into a struct.<br>
The change was for convenience, but I ended up with code taking 4x longer to execute.<br>
Since the size of a struct and the starting locations of all elements (to the best of my knowledge) are known at compile time, <br>
I expected member access to be replaced by a memory access (calculated at compile time) to that element.<br>
If we don't expose them (e.g. with repr(c)) array elements might not be stored strictly in the way in which they are named in the struct.<br>
Beside of that, I don't expect much "magic" to happen there.<br>
Therefore my questions now are:<br>
Is this expectation (roughly) true,<br>
at which level (HIR, MIR, LLVM-IR) does this translation happen,<br>
which code modifications are applied before the struct is removed from the code ( since I guess such a transition is causing the performance difference)?</p>
<p>Just to get a vague feeling for places to look into.<br>
I'm not too experienced with rust and especially the compiler, so if there is a better way to hunt down this performance issue, please let me know.</p>



<a name="241964518"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/struct%20usage%20leading%20to%20poor%20performance/near/241964518" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nikita Popov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/struct.20usage.20leading.20to.20poor.20performance.html#241964518">(Jun 08 2021 at 18:27)</a>:</h4>
<p>Typically structs are broken down by SROA in LLVM (assuming the struct is used for a local variable).</p>



<a name="241964690"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/struct%20usage%20leading%20to%20poor%20performance/near/241964690" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nikita Popov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/struct.20usage.20leading.20to.20poor.20performance.html#241964690">(Jun 08 2021 at 18:28)</a>:</h4>
<p>For structs containing arrays in particular, a common issue is aliasing. LLVM will generally not know that accesses to one array can't overflow into the other array.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>